<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="query" clearable placeholder="请输入内容" class="input-with-select" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="handleSearch" />
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" icon="el-icon-plus" @click="handleAddGood">增加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsData" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="50px" align="center" />
        <el-table-column prop="name" label="商品名称" align="center" />
        <el-table-column prop="price" label="商品价格(元)" width="150px" align="center" />
        <el-table-column prop="number" label="商品库存" width="150px" align="center" />
        <el-table-column label="图片" width="200px" align="center">
          <template v-slot="scope">
            <img :src="scope.row.image_url" alt="" style="width: 180px">
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-show="pageshow"
        :current-page="page"
        :page-sizes="[5,10, 20]"
        :page-size="pagesize"
        layout=" prev, pager, next, jumper,->,sizes,total"
        :total="total"
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { goodsList, delGoods } from '@/api/goods'

export default {
  name: 'GoodsList',
  data() {
    return {
      goodsData: [],
      query: '',
      page: 1,
      pagesize: 5,
      total: 0,
      totalPages: 0,
      pageshow: true
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const res = await goodsList({ query: this.query, page: this.page, pagesize: this.pagesize })
      if (res.code !== 200) return this.$message.error('获取商品列表失败！')
      this.goodsData = res.data.lists
      this.total = res.data.total
      this.totalPages = res.data.pages
    },
    // 商品查询
    handleSearch() {
      this.page = 1
      this.pageshow = false
      this.getGoodsList()
      this.$nextTick(() => {
        this.pageshow = true
      })
    },
    // 商品编辑操作，打开商品弹框
    handleEdit(row) {
      this.$message.info('该功能暂时未作尽请期待啊！')
    },
    // 商品删除操作
    async handleDelete(row) {
      const res = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((e) => e)
      if (res === 'cancel') return this.$message.info('已取消删除操作！')
      const result = await delGoods(row.id)
      if (result.code !== 200) return this.$message.error('删除商品失败！')
      this.$message.success('删除商品成功！')
      const totalPages = Math.ceil((this.total - 1) / this.pageSize)
      this.page = this.page > totalPages ? totalPages : this.page
      this.page = this.page < 1 ? 1 : this.page
      this.getGoodsList()
    },
    // 增加商品跳转
    handleAddGood() {
      this.$router.push('/addGood')
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getGoodsList()
    }
  }
}
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}
</style>
